<?php

namespace app\admin\controller;
use think\Loader;
use think\Config;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\User as UserModel;
class Member extends Base{
  public function initialize(){
    parent::initialize();
    $this->Model = new UserModel();
    $this->user_type = "member";
  }

  public function index(){
    $param = [];
    $param = $this->request->param();
    $res = $this->Model->memberPaginate($param);
    // exit();
    $this->assign("param", $param);
    $this->assign("res", $res);
    return $this->fetch();
  }

  public function create(){
    if($this->request->isPost()){
      $data = $this->getPostData();
      $res = $this->Model->saveItem($data);
      if($res["id"]){
        $this->success($res["msg"], 'index');
      }else{
        $this->error($res["msg"]);
      }
    }else{
      $user = ["total_score" => 0, "active_score" => 0, "total_coin" => 0, "active_coin" => 0];
      $this->assign("user", $user);
      return $this->fetch("form");
    }
  }

  /**
   * 显示指定的资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function read($id){
    $user = $this->Model->where("id", $id)->find();
    if(empty($user)){
      $this->redirect('index');
    }
  }

  /**
   * 显示编辑资源表单页.
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function edit($id){
    $user = $this->Model->where("id", $id)->where("user_type", $this->user_type)->find();
    if(empty($user)){
      $this->redirect('index');
    }

    if($this->request->isPost()){
      $data = $this->getPostData();
      $res = $this->Model->updateBaseInfo($data);
      if($res["status"]){
        $this->success($res["msg"], 'index');
      }else{
        $this->error($res["msg"]);
      }
    }else{
      $this->assign("user", $user);
      return $this->fetch("form");
    }
  }

  /**
   * 删除指定资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function delete($id){
    $this->error("您访问的页面不存在", "index");
  }

  public function lock($id){
    if($this->request->isDelete()){
      $data = ["id" => $id, "user_type" => $this->user_type, "is_locked" => true];
      $res = $this->Model->updateBaseInfo($data);
      if($res["status"]){
        $this->success($res["msg"], 'index');
      }else{
        $this->error($res["msg"]);
      }
    }else{
      $this->error("请求方式不正确", 'index');
    }
  }

  public function unlock($id){
    if($this->request->isDelete()){
      $data = ["id" => $id, "user_type" => $this->user_type, "is_locked" => false];
      $res = $this->Model->updateBaseInfo($data);
      if($res["status"]){
        $this->success($res["msg"], 'index');
      }else{
        $this->error($res["msg"]);
      }
    }else{
      $this->error("请求方式不正确", 'index');
    }
  }

  /**
   * 修改用户类型
   * @Author zhanghong
   * @Date   2017-04-23
   * @return [type]     [description]
   */
  public function change_type($id){
    $user = $this->Model->where("id", $id)->where("user_type", $this->user_type)->find();
    if(empty($user)){
      $this->redirect('index');
    }

    if($this->request->isPost()){
      $group_id = $this->request->post("manager_group_id");
      $res = $this->Model->changeUserType($id, "manager", $group_id);
      if($res["status"]){
        $this->success($res["msg"], 'index');
      }else{
        $this->error($res["msg"]);
      }
    }else{
      $GroupModel = new AuthGroupModel();
      $groups = $GroupModel->selectTypeAll("manager");
      $this->assign("groups", $groups);
      $this->assign("user", $user);
      return $this->fetch();
    }
  }

  public function reset_password(){

  }

  private function getPostData(){
    $fields = [
      ['name' => 'id', 'type' => 'integer', 'default' => '', 'is_null' => true],
      ['name' => 'email', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'nickname', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'gender', 'type' => 'string', 'default' => 'unknown', 'is_null' => false],
      ['name' => 'mobile', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'url', 'type' => 'string', 'default' => '', 'is_null' => false],
      ['name' => 'birthday', 'type' => 'date', 'default' => NULL, 'is_null' => false],
      ['name' => 'manager_group_id', 'type' => 'integer', 'default' => '', 'is_null' => false],
      ['name' => 'active_score', 'type' => 'integer', 'default' => 0, 'is_null' => false],
      ['name' => 'active_coin', 'type' => 'integer', 'default' => 0, 'is_null' => false],
    ];
    $atl_name = $this->request->action();
    if($atl_name == "create"){
      array_push($fields, ['name' => 'password', 'type' => 'string', 'default' => '', 'is_null' => false]);
      array_push($fields, ['name' => 'confirm_password', 'type' => 'string', 'default' => '', 'is_null' => false]);
    }
    $data = $this->parsePostParam($fields);
    return array_merge($data, ['user_type' => $this->user_type]);
  }
}
